package com.lk.mi.mapper;

import com.lk.mi.entity.item;
import com.lk.mi.entity.shopCart;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface shopCartMapper {


    /**
     * 根据用户id查询他的购物车
     * @return
     */
    @Select("select * from shopcart where user_id = #{id}")
    List<shopCart> findCart(Integer id);

    /**
     * 添加先查询
     * @param iid
     * @return
     */
    @Select("select * from items where id=#{iid}")
    item addOne(Integer iid);

    @Select("select * from shopcart where user_id = #{uid} and item_id = #{iid}")
    shopCart findByTwo(@Param("uid") Integer uid,@Param("iid") Integer iid);

    /**
     * 添加购物车
     * @param shopCart
     * @return
     */
    @Insert("insert into shopCart values (0,#{userId},#{itemId},#{itemImage},#{itemName},#{price},#{number},#{amount})")
    Integer add(shopCart shopCart);

    /**
     * 根据用户id和商品id查询购物车，对该商品数量加一
     * @param uid
     * @param iid
     * @return
     */
    @Update("update shopcart set number = number +1,amount = amount+price where user_id=#{uid} and item_id = #{iid}")
    Integer updateByUidAndIid(@Param("uid") Integer uid,@Param("iid") Integer iid);


    /**
     * 根据用户id和商品id查询购物车，对该商品数量减一
     * @param uid
     * @param iid
     * @return
     */
    @Update("update shopcart set number = number -1,amount = amount-price where user_id=#{uid} and item_id = #{iid}")
    Integer updateByUidAndIid2(@Param("uid") Integer uid,@Param("iid") Integer iid);



    /**
     * 删除购物车(根据id删除)
     * @param id
     * @return
     */
    @Delete("delete from shopCart where id =#{id}")
    Integer delete(Integer id);


    /**
     * 根据id查询该用户购物车里的所有的商品
     * @param uid
     * @return
     */
    @Select("select * from shopcart where user_id = #{uid}")
    List<shopCart> findSbAllShopCart(Integer uid);

    //TODO  购物车功能完善

}
